package com.triones.yjj.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.triones.yjj.entity.MemberGroupPurchaseDO;
import com.triones.yjj.entity.OrderDO;
import com.triones.yjj.entity.bo.BatchVirtualBO;
import com.triones.yjj.entity.bo.MemberBringOrderListParams;
import com.triones.yjj.entity.bo.MemberDistributionParams;
import com.triones.yjj.entity.bo.OrderListParams;
import com.triones.yjj.entity.vo.DistributionOrderVO;
import com.triones.yjj.entity.vo.OrderDetailVO;
import com.triones.yjj.entity.vo.OrderListVO;
import com.triones.yjj.service.OrderService;
import com.triones.yjj.service.WxPayService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 订单管理
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/order")
public class OrderController extends BaseController {

    private final OrderService orderService;

    private final WxPayService wxPayService;

    /**
     * 查询订单列表（分页）
     */
    @GetMapping("/pageList")
    public TableDataInfo<OrderListVO> pageList(OrderListParams params, PageQuery pageQuery) {
        return orderService.pageList(params, pageQuery);
    }

    /**
     * 订单统计
     */
    @GetMapping("/stats")
    public R<Map> stats(OrderListParams params) {
        return R.ok(orderService.stats(params));
    }

    /**
     * 订单详情
     */
    @GetMapping("/{orderId}")
    public R<OrderDetailVO> detailById(@PathVariable Long orderId) {
        return R.ok(orderService.detailById(orderId));
    }

    /**
     * 获取客户某个活动带来的订单列表（分页）
     */
    @GetMapping("/bringOrder/pageList")
    public TableDataInfo<OrderListVO> bringOrderPageList(MemberBringOrderListParams params, PageQuery pageQuery) {
        return orderService.bringOrderPageList(params, pageQuery);
    }

    /**
     * 订单核销
     */
    @PostMapping("/verification")
    public R<Void> orderVerification(@Validated @RequestBody OrderDO dto) {
        if (dto.getVerificationCode() == null) {
            throw new SecurityException("核销码不能为空");
        }
        return toAjax(orderService.orderVerification(dto));
    }

    /**
     * 退款
     */
    @PostMapping("/refund/{orderId}")
    public R<Void> refund(@NotNull(message = "订单id不能为空") @PathVariable Long orderId) {
        return toAjax(wxPayService.refund(orderId));
    }

    /**
     * 升为团长
     */
    @PostMapping("/upgrade/{orderId}")
    public R<Void> upgrade(@NotNull(message = "订单id不能为空") @PathVariable Long orderId) {
        return toAjax(orderService.upgrade(orderId));
    }

    /**
     * 降为团员
     */
    @PostMapping("/reduce/{orderId}")
    public R<Void> reduce(@NotNull(message = "订单id不能为空") @PathVariable Long orderId) {
        return toAjax(orderService.reduce(orderId));
    }

    /**
     * 独立成团
     */
    @PostMapping("/isolate/{orderId}")
    public R<Void> isolate(@NotNull(message = "订单id不能为空") @PathVariable Long orderId) {
        return toAjax(orderService.isolate(orderId));
    }

    /**
     * 批量退款
     */
    @PostMapping("/batchRefund/{ids}")
    public R<Void> batchRefund(@PathVariable Long[] ids) {
        return toAjax(wxPayService.batchRefund(ids));
    }

    /**
     * 查询订单列表（分页）
     */
    @GetMapping("/distributionOrder/pageList")
    public TableDataInfo<DistributionOrderVO> distributionOrderPageList(MemberDistributionParams params, PageQuery pageQuery) {
        return orderService.distributionOrderPageList(params, pageQuery);
    }

    /**
     * 移团
     */
    @PostMapping("/moveToTeam/{groupPurchaseId}/{orderId}")
    public R<Void> moveToTeam(@NotNull(message = "目标团不能为空") @PathVariable Long groupPurchaseId,
                              @NotNull(message = "用户订单不能为空") @PathVariable Long orderId) {
        return toAjax(orderService.moveToTeam(groupPurchaseId,orderId));
    }

    /**
     * 修改用户报名信息
     */
    @PostMapping("/editEnroll")
    public R<Void> editEnroll(@Validated @RequestBody OrderDO dto) {
        return toAjax(orderService.editEnroll(dto));
    }

    /**
     * 调整虚拟团员数量
     */
    @PostMapping("/virtual/{orderId}")
    public R<Void> virtual(@NotNull(message = "目标团不能为空") @PathVariable Long orderId,@RequestBody MemberGroupPurchaseDO dto) {
        return toAjax(orderService.virtual(orderId,dto));
    }

    /**
     * 批量调整虚拟团员数量
     */
    @PostMapping("/batchVirtual")
    public R<Void> batchVirtual(@RequestBody BatchVirtualBO bo) {
        return toAjax(orderService.batchVirtual(bo));
    }
}
